/*******************************************************************************
 * Copyright (c) 2011, 2013 Profactor GmbH, fortiss GmbH
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Gerhard Ebenhofer, Alois Zoitl - initial API and implementation and/or initial documentation
 *******************************************************************************/
#include "externtimeha.h"
#include "forte/devexec.h"
#include <windows.h>

using namespace forte::literals;

namespace forte::arch {
  CExternTimerHandler *CExternTimerHandler::smFORTEExtTimer = nullptr;

  const TForteInt32 CExternTimerHandler::csmTicksPerSecond = 1000;

  extern "C" __declspec(dllexport) void __stdcall nextTick(void) {
    CExternTimerHandler::externNextTick();
  }

  extern "C" __declspec(dllexport) unsigned int __stdcall getTicksPerSecond() {
    return CExternTimerHandler::getExternTicksPerSecond();
  }

  CExternTimerHandler::CExternTimerHandler(CDeviceExecution &paDeviceExecution) : CTimerHandler(paDeviceExecution) {
    if (CExternTimerHandler::smFORTEExtTimer != nullptr) { // creating two timers is not possible
      CExternTimerHandler::smFORTEExtTimer = this;
    }
  }

  CExternTimerHandler::~CExternTimerHandler() {
    disableHandler();
    CExternTimerHandler::smFORTEExtTimer = nullptr;
  }

  void CExternTimerHandler::externNextTick() {
    if (CExternTimerHandler::smFORTEExtTimer) {
      CExternTimerHandler::smFORTEExtTimer->nextTick();
    }
  }

  void CExternTimerHandler::enableHandler() {
    // TODO think on hwo to handle this.
  }

  void CExternTimerHandler::disableHandler() {
    // TODO think on hwo to handle this.
  }
} // namespace forte::arch
